logo

Cheat Sheet for using Twig Tweak in Drupal 10

15
April

Cheat Sheet for using Twig Tweak in Drupal 10
By: Anonymous | Published On: Mon, 04/15/2024 - 17:52

1. **Render a View**:

- Use `drupal_view('view_name', 'display_id')` to embed a view.

- Replace `'view_name'` with the actual view name and `'display_id'` with the desired display (default is `'default'`).

- Example: `{{ drupal_view('who_s_new', 'block_1') }}.

2. **Get the Result of a View**:

- Retrieve the result of a view using `drupal_view_result('view_name', 'display_id')`.

- Parameters are the same as for rendering a view.

- Example: `{{ drupal_view_result('who_s_new', 'block_1') }}.

3. **Render a Block**:

- Render a block using `drupal_block('block_id')`.

- Replace `'block_id'` with the actual block ID.

   - Examples:
     - Plugin Block: `{{ drupal_block('my_plugin_block') }}`

 

- Configuration Entity Block: `{{ drupal_block('my_config_entity_block') }}` - Content Entity Block: `{{ drupal_block('my_content_entity_block') }}.

4. **Render a Region**:

- Display contents of a region from the default or any specified theme.

- Use `drupal_region('region_name', 'theme_name')`.

   - Example:
     - Default theme: `{{ drupal_region('sidebar_first') }}`
     - Specific theme (e.g., Bartik): `{{ drupal_region('sidebar_first', 'bartik') }}.
	

5. **Render an Entity**:

- Render an entity using `drupal_entity('entity_type', entity_id, 'view_mode')`.

- Replace `'entity_type'` with the entity type (e.g., `'node'`, `'block_content'`).

   - Example:
     - Node teaser: `{{ drupal_entity('node', 123, 'teaser') }}`
     - Custom block: `{{ drupal_entity('block', 'my_custom_block', check_access=false) }}.
	

6. **Get an Entity Form**:

- Retrieve the built and processed entity form using `drupal_entity_form('entity_type', entity_id, 'form_mode', values)`.

   - Example:
     - New entity form: `{{ drupal_entity_form('node', null, 'default') }}`
     - Edit entity form: `{{ drupal_entity_form('node', 123, 'edit', {'title': 'New Title'}) }}.
	

Need Help ?